<?php
/**
 * 会员中心——账户概览
 *
 *
 *
 *
 * @copyright  Copyright (c) 2014-2015 TuntronKJ Inc. (http://www.tuntron.com)
 * @license    http://www.tuntron.com
 * @link       http://www.tuntron.com
 * @since      File available since Release v1.1
 */
defined('InTuntronKJ') or exit('Access Invalid!');

class memberControl extends BaseMemberControl{

	/**
	 * 输出会员信息
	 *
	 * @param
	 * @return
	 */
	public function GetUserInfoOp() {
		$user_model = Model('member');
		$userinfo = $user_model->getMemberInfo(array('uid'=>$_SESSION['uid']));
		echo json_encode($userinfo);
		exit();
//	{"uid":"1","authority":"","isDelete":"0","enable":"1","parentId":"1","parents":"1,1","admin":"1","username":"admin","password":"e10adc3949ba59abbe56e057f20f883e","coinPassword":"c33367701511b4f6020ec61ded352059","type":"1","subCount":"0","sex":"\u4fdd\u5bc6","nickname":"","name":"","regIP":"2130706433","regTime":"1373525996","updateTime":"2014-11-28 00:37:14","province":"","city":"","address":"","qq":"","msn":"","mobile":"","email":"","idCard":"","grade":"1","score":"702652","scoreTotal":"709152","coin":"8226.2305","fcoin":"-200.0000","fanDian":"15.0","fanDianBdw":"5.0","safepwd":"","safeEmail":""}
//		echo '{"Id":167009,"username":"haossc","uermoney":0.0000,"allmoney":0.0000,"UserType":"会员","UserPassword":"4cbd23bd5be2f7def376d9768d336cde","FundPassword":"dd46d03c762d3e7226c63311ccc02f89","GamePoint":8.2900,"AvailablePoint":8.2900,"FreezePoint":0.0000,"NickName":"haossc","Pid":133104,"ReturnPoint":0.035,"Activated":1,"Email":"haossc@qq.com","Phone":null,"RealName":null,"Address":"","Token":null,"LastLogonTime":"\/Date(1420601387083)\/","SortOrder":null,"LastOnlineTime":null,"Hierarchy":"\u0026大金一线\u0026\u0026大金一叶知秋\u0026","RegistedDateTime":"\/Date(1417699828917)\/","UserLevel":0,"MD5UserName":"0x894661ed3147518adb4516f7fc94e576","SocketInfo":null,"DateInserted":"\/Date(1417699828917)\/","DateModified":"\/Date(1417699828917)\/","CreatedUser":null,"UpdatedUser":null,"LastLoginIp":"60.172.68.192","VerifyCode":0}';
	}


	/**
	 * 会员地址
	 *
	 * @param
	 * @return
	 */
	public function addressOp() {
		/**
		 * 读取语言包
		 */
		Language::read('member_member_index');
		$lang	= Language::getLangContent();
		/**
		 * 实例化模型
		 */
		$address_class = Model('address');
		/**
		 * 判断页面类型
		 */
		if (!empty($_GET['type'])){
			/**
			 * 新增/编辑地址页面
			 */
			if (intval($_GET['id']) > 0){
				/**
				 * 得到地址信息
				 */
				$address_info = $address_class->getOneAddress(intval($_GET['id']));
				if ($address_info['member_id'] != $_SESSION['member_id']){
					showMessage($lang['member_address_wrong_argument'],'index.php?ac=member&op=address','html','error');
				}
				/**
				 * 输出地址信息
				 */
				Tpl::output('address_info',$address_info);
			}
			/**
			 * 增加/修改页面输出
			 */
			Tpl::output('type',$_GET['type']);
			Tpl::showpage('address_form','null_layout');
			exit();
		}
		/**
		 * 判断操作类型
		 */
		if (chksubmit()){
			/**
			 * 验证表单信息
			 */
			$obj_validate = new Validate();
			$obj_validate->validateparam = array(
				array("input"=>$_POST["true_name"],"require"=>"true","message"=>$lang['member_address_receiver_null']),
				array("input"=>$_POST["area_id"],"require"=>"true","validator"=>"Number","message"=>$lang['member_address_wrong_area']),
				array("input"=>$_POST["city_id"],"require"=>"true","validator"=>"Number","message"=>$lang['member_address_wrong_area']),
				array("input"=>$_POST["area_info"],"require"=>"true","message"=>$lang['member_address_area_null']),
				array("input"=>$_POST["address"],"require"=>"true","message"=>$lang['member_address_address_null']),
				array("input"=>$_POST['tel_phone'].$_POST['mob_phone'],'require'=>'true','message'=>$lang['member_address_phone_and_mobile'])
			);
			$error = $obj_validate->validate();
			if ($error != ''){
				showValidateError($error);
			}
            $data = array();
            $data['member_id'] = $_SESSION['member_id'];
            $data['true_name'] = $_POST['true_name'];
            $data['area_id'] = intval($_POST['area_id']);
            $data['city_id'] = intval($_POST['city_id']);
            $data['area_info'] = $_POST['area_info'];
            $data['address'] = $_POST['address'];
            $data['tel_phone'] = $_POST['tel_phone'];
            $data['mob_phone'] = $_POST['mob_phone'];

			if (intval($_POST['id']) > 0){
                $rs = $address_class->editAddress($data, array('address_id' => $_POST['id']));
				if (!$rs){
					showDialog($lang['member_address_modify_fail'],'','error');
				}
			}else {
				$rs = $address_class->addAddress($data);
				if (!$rs){
					showDialog($lang['member_address_add_fail'],'','error');
				}
			}
			showDialog($lang['nc_common_op_succ'],'reload','succ','CUR_DIALOG.close()');
		}
		$del_id = isset($_GET['id']) ? intval(trim($_GET['id'])) : 0 ;
		if ($del_id > 0){
			$rs = $address_class->delAddress(array('address_id'=>$del_id,'member_id'=>$_SESSION['member_id']));
			if ($rs){
				showDialog(Language::get('member_address_del_succ'),'index.php?ac=member&op=address','succ');
			}else {
				showDialog(Language::get('member_address_del_fail'),'','error');
			}
		}
		$address_list = $address_class->getAddressList(array('member_id'=>$_SESSION['member_id']));
		/**
		 * 页面输出
		 */
		self::profile_menu('address','address');
		Tpl::output('menu_sign','address');
		Tpl::output('address_list',$address_list);
		Tpl::output('menu_sign_url','index.php?ac=member&op=address');
		Tpl::output('menu_sign1','address_list');
		Tpl::setLayout('member_pub_layout');
		Tpl::showpage('address_index');
	}

	
	/**
	 * 设置银行帐户
	 */
	public function SetCBAccountOp(){
		$urlshang = $_SERVER['HTTP_REFERER']; //上一页URL   
//		$urlshang = getReferer();
		$urldan = $_SERVER['SERVER_NAME']; //本站域名
		$urlcheck=substr($urlshang,7,strlen($urldan));
		if($urlcheck<>$urldan){
			Re_json_encode('-100','数据包被篡改，请重新操作!');
		}
		
		if(!$_POST) Re_json_encode('-100','参数出错!');
		
		$uid = $_SESSION['uid'];
		$para['bankId']=intval($_POST['bankInfoPayType']);
		$para['account']=trim($_POST['bankInfoNum']);
		$para['username']=trim($_POST['bankInfoUserName']);
		$para['uid'] = $uid;
		$para['editEnable']=0;
		
		$model_bank = Model('bank');
		$model_member = Model('member');		
		$memberinfo = $model_member->getMemberInfo(array('uid'=>$uid));
		$paymentinfo = $model_bank->getPaymentInfo(array('uid'=>$uid));
		
		if(!empty($bankinfo) && is_array($bankinfo)){
			$output = json_encode("hasbind");
			echo $output;
			exit();
		}elseif(md5($_POST['bankPassword']) == $memberinfo['password']){
			$output = json_encode("samePassword");
			echo $output;
			exit();
		}else{
			$insert = $model_bank->addPayment($para);
			if($insert){
				$model_member->updateMember(array('coinPassword'=>md5($_POST['bankPassword'])),$uid);
				$output = json_encode("suc");
			}
			echo $output;
			exit();
		}

	}
	
	
	/**
	 * 更新用户登录密码
	 * Enter description here ...
	 */
	public function ChangePasswordOp(){
		
		$urlshang = $_SERVER['HTTP_REFERER']; //上一页URL   
		$urldan = $_SERVER['SERVER_NAME']; //本站域名
		$urlcheck=substr($urlshang,7,strlen($urldan));
		if($urlcheck<>$urldan){
			Re_json_encode('-100','数据包被篡改，请重新操作!');
		}
				
		$oldPwd = $_POST['oldPwd'];
		$newPwd1 = $_POST['newPwd1'];
		$newPwd2 = $_POST['newPwd2'];
		
		$model_member = Model('member');		
		$memberinfo = $model_member->getMemberInfo(array('uid'=>$_SESSION['uid']));
			
		if(!$oldPwd){
			$output = json_encode("原密码不能为空！");
		}elseif(!$newPwd1){
			$output = json_encode("新密码不能为空！");
		}elseif($newPwd1 != $newPwd2){
			$output = json_encode("密码不一致，请重新输入!");		
		}elseif($memberinfo['password'] != md5($oldPwd)){
			$output = json_encode("原密码不正确!");
		}else{
			
			$update = $model_member->updateMember(array('password'=>md5($newPwd1)),$memberinfo['uid']);
			if($update){
				$output = json_encode("修改密码成功");
			}else{
				$output = json_encode("修改密码失败，请重新输入");
			}
			
		}
		echo $output;
		exit();
		
	}
	
	
	/**
	 * 更新用户资金密码
	 */
	public function ChangeFundPasswordOp(){
		$urlshang = $_SERVER['HTTP_REFERER']; //上一页URL   
		$urldan = $_SERVER['SERVER_NAME']; //本站域名
		$urlcheck=substr($urlshang,7,strlen($urldan));
		if($urlcheck<>$urldan){
			Re_json_encode('-100','数据包被篡改，请重新操作!');
		}
				
		$oldPwd = $_POST['oldPwd'];
		$newPwd1 = $_POST['newPwd1'];
		$newPwd2 = $_POST['newPwd2'];
		
		$model_member = Model('member');		
		$memberinfo = $model_member->getMemberInfo(array('uid'=>$_SESSION['uid']));
			
		if(!$oldPwd){
			$output = json_encode("原密码不能为空！");
		}elseif(!$newPwd1){
			$output = json_encode("新密码不能为空！");
		}elseif($newPwd1 != $newPwd2){
			$output = json_encode("密码不一致，请重新输入!");		
		}elseif($memberinfo['coinPassword'] != md5($oldPwd)){
			$output = json_encode("原密码不正确!");
		}else{			
			$update = $model_member->updateMember(array('coinPassword'=>md5($newPwd1)),$memberinfo['uid']);
			if($update){
				$output = json_encode("修改密码成功");
			}else{
				$output = json_encode("修改密码失败，请重新输入");
			}
			
		}
		echo $output;
		exit();
	}
	
	
	/**
	 * 用户中心右边，小导航
	 *
	 * @param string	$menu_type	导航类型
	 * @param string 	$menu_key	当前导航的menu_key
	 * @return
	 */
	private function profile_menu($menu_type,$menu_key='') {
		/**
		 * 读取语言包
		 */
		Language::read('member_layout');
		$menu_array	= array();
		switch ($menu_type) {
			case 'address':
				$menu_array = array(
				1=>array('menu_key'=>'address','menu_name'=>Language::get('nc_member_path_address_list'),	'menu_url'=>'index.php?ac=member&op=address'));
				break;
			case 'member_order':
				$menu_array = array(
				1=>array('menu_key'=>'member_order','menu_name'=>Language::get('nc_member_path_order_list'),	'menu_url'=>'index.php?ac=member_order'),
				2=>array('menu_key'=>'buyer_refund','menu_name'=>Language::get('nc_member_path_buyer_refund'),	'menu_url'=>'index.php?ac=member_refund'),
				3=>array('menu_key'=>'buyer_return','menu_name'=>Language::get('nc_member_path_buyer_return'),	'menu_url'=>'index.php?ac=member_return'));
				break;
		}
		Tpl::output('member_menu',$menu_array);
		Tpl::output('menu_key',$menu_key);
	}
}
